package uncharteddal

import (
	"fmt"

	"public.com/goutil/logUtil"

	"7qule.com/sd3/CrossServer/src/dal"
	"7qule.com/sd3/CrossServer/src/model/redisKey"
	"7qule.com/sd3/CrossServer/src/model/uncharted"
	"github.com/garyburd/redigo/redis"
)

// 获取玩家的分数信息
// playerId:玩家Id
// 返回值:
// err:错误信息
// exist:是否存在对应模块
// mapId:地图Id
// star:分数
// fightTime:最近一次挑战时间
func GetPlayerScore(playerId string) (err error, exist bool, scoreInfo *uncharted.UnchartedPlayerScoreInfo) {
	conn := dal.GetRedisPool().GetConnection()
	defer conn.Close()
	var reply interface{}

	reply, err = conn.Do("HMGET", redisKey.GetPlayerKey(playerId),
		redisKey.Player_ServerGroupId,
		redisKey.Uncharted_MapId,
		redisKey.Uncharted_Star,
		redisKey.Uncharted_Score,
		redisKey.Uncharted_TempTerritoryScore,
		redisKey.Uncharted_ScoreChangeTime)
	if err != nil {
		logUtil.Log(fmt.Sprintf("从Redis中获取玩家uncharted信息出错1，错误信息为：%s", err), logUtil.Error, true)
		return
	}

	values, err := redis.Values(reply, err)
	if err != nil {
		logUtil.Log(fmt.Sprintf("从Redis中获取玩家uncharted信息出错2，错误信息为：%s", err), logUtil.Error, true)
		return
	}

	var (
		serverGroupId      int
		mapId              int8
		star               int
		score              int
		tempTerritoryScore int
		scoreChangeTime    int64
	)
	_, err = redis.Scan(values, &serverGroupId, &mapId, &star, &score, &tempTerritoryScore, &scoreChangeTime)
	if err != nil {
		if err == redis.ErrNil {
			// 找不到的错误不能认定为错误，通过exist可以表达出这个意思
			err = nil
			return
		} else {
			logUtil.Log(fmt.Sprintf("从Redis中获取玩家uncharted信息出错3，错误信息为：%s", err), logUtil.Error, true)
			return
		}
	}

	exist = true

	// 返回新建的对象
	scoreInfo = uncharted.NewUnchartedPlayerScoreInfo(playerId, serverGroupId, mapId, star, score, tempTerritoryScore, scoreChangeTime)

	return
}

// 获取玩家所有数据
// playerId:玩家Id
// conn:数据库连接对象
func getPlayerAllInfo(playerId string, conn redis.Conn) (err error, exist bool, result *uncharted.MatchPlayer) {
	key := redisKey.GetPlayerKey(playerId)
	reply, err := conn.Do("HMGET",
		key,
		redisKey.Player_Name,
		redisKey.Player_Lv,
		redisKey.Player_Vip,
		redisKey.Player_FAP,
		redisKey.Player_ServerGroupId,
		redisKey.Player_PartnerId,
		redisKey.Player_ServerId,
		redisKey.Player_Zone,
		redisKey.Player_HeadImageId,
		redisKey.Player_FashionModelId,
		redisKey.Player_PVPInterLv,
		redisKey.Player_GuildName,
		redisKey.Player_DiyHeadPortraitId,
		redisKey.Uncharted_MapId,
		redisKey.Uncharted_Star,
		redisKey.Uncharted_Score,
		redisKey.Uncharted_TempTerritoryScore,
		redisKey.Uncharted_ScoreChangeTime,
	)

	if err != nil {
		logUtil.Log(fmt.Sprintf("uncharted 从Redis中获取玩家信息出错1，错误信息为：%s", err), logUtil.Error, true)
		return err, false, nil
	}

	values, err := redis.Values(reply, err)
	if err != nil {
		logUtil.Log(fmt.Sprintf("uncharted 从Redis中获取玩家信息出错2，错误信息为：%s", err), logUtil.Error, true)
		return err, false, nil
	}

	result = &uncharted.MatchPlayer{
		Id: playerId,
	}

	var (
		mapId              int8
		star               int
		score              int
		tempTerritoryScore int
		scoreChangeTime    int64
	)

	if _, err = redis.Scan(values, &result.Name, &result.Lv, &result.Vip, &result.FAP,
		&result.ServerGroupId, &result.PartnerId, &result.ServerId, &result.Zone,
		&result.HeadImageId, &result.FashionModelId, &result.PVPInterLv,
		&result.GuildName, &result.DiyHeadPortraitId, &mapId, &star, &score, &tempTerritoryScore, &scoreChangeTime); err != nil {
		logUtil.Log(fmt.Sprintf("uncharted 从Redis中获取玩家基本信息出错3，错误信息为：%s", err), logUtil.Error, true)
		return err, true, nil
	}

	result.ScoreInfo = uncharted.NewUnchartedPlayerScoreInfo(playerId, result.ServerGroupId, mapId,
		star, score, tempTerritoryScore, scoreChangeTime)
	return nil, true, result
}

// 获取玩家的分数信息
// playerId:玩家Id
// 返回值:
// err:错误信息
// exist:是否存在对应模块
// mapId:地图Id
// star:分数
// fightTime:最近一次挑战时间
func UpdateFightInfo(scoreInfo *uncharted.UnchartedPlayerScoreInfo) (err error) {
	conn := dal.GetRedisPool().GetConnection()
	defer conn.Close()
	_, err = conn.Do("HMSET", redisKey.GetPlayerKey(scoreInfo.PlayerId),
		redisKey.Uncharted_MapId, scoreInfo.MapId,
		redisKey.Uncharted_Star, scoreInfo.Star,
		redisKey.Uncharted_Score, scoreInfo.Score,
		redisKey.Uncharted_ScoreChangeTime, scoreInfo.ScoreChangeTime)
	if err != nil {
		logUtil.Log(fmt.Sprintf("Redis 更新玩家uncharted信息出错1，错误信息为：%s", err), logUtil.Error, true)
		return
	}

	return
}

// 更新玩家的分数信息
// playerId:玩家Id
// 返回值:
// err:错误信息
func UpdatePlayerScore(scoreInfo *uncharted.UnchartedPlayerScoreInfo) (err error) {
	conn := dal.GetRedisPool().GetConnection()
	defer conn.Close()
	_, err = conn.Do("HMSET", redisKey.GetPlayerKey(scoreInfo.PlayerId),
		redisKey.Uncharted_MapId, scoreInfo.MapId,
		redisKey.Uncharted_Star, scoreInfo.Star,
		redisKey.Uncharted_Score, scoreInfo.Score,
		redisKey.Uncharted_TempTerritoryScore, scoreInfo.TempTerritoryScore,
		redisKey.Uncharted_ScoreChangeTime, scoreInfo.ScoreChangeTime)
	if err != nil {
		logUtil.Log(fmt.Sprintf("Redis 更新玩家uncharted信息出错1，错误信息为：%s", err), logUtil.Error, true)
		return
	}

	return
}

// 保存匹配玩家信息
// matchPlayer：匹配玩家列表
// 返回值:
// error:处理错误信息
func SaveMatchPlayer(matchPlayer *uncharted.MatchPlayer) error {
	conn := dal.GetRedisPool().GetConnection()
	defer conn.Close()

	_, err := conn.Do("HMSET", redisKey.GetPlayerKey(matchPlayer.Id),
		redisKey.Player_Name, matchPlayer.Name,
		redisKey.Player_Lv, matchPlayer.Lv,
		redisKey.Player_Vip, matchPlayer.Vip,
		redisKey.Player_FAP, matchPlayer.FAP,
		redisKey.Player_MaxFap, matchPlayer.MaxFap,
		redisKey.Player_ServerGroupId, matchPlayer.ServerGroupId,
		redisKey.Player_PartnerId, matchPlayer.PartnerId,
		redisKey.Player_ServerId, matchPlayer.ServerId,
		redisKey.Player_Zone, matchPlayer.Zone,
		redisKey.Player_HeadImageId, matchPlayer.HeadImageId,
		redisKey.Player_FashionModelId, matchPlayer.FashionModelId,
		redisKey.Player_PVPInterLv, matchPlayer.PVPInterLv,
		redisKey.Player_GuildName, matchPlayer.GuildName,
		redisKey.Player_DiyHeadPortraitId, matchPlayer.DiyHeadPortraitId,
	)
	if err != nil {
		logUtil.Log(fmt.Sprintf("设置玩家匹配信息出错，错误信息为：%s", err), logUtil.Error, true)
		return err
	}

	return nil
}
